/* 
 * Copyright (c) 2012, Fromentin Xavier, Schnell Michaël, Dervin Cyrielle, Brabant Quentin
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *      * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *      * The names of its contributors may not be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Fromentin Xavier, Schnell Michaël, Dervin Cyrielle OR Brabant Quentin 
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package kameleon.document;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import kameleon.exception.InvalidIndexException;

/**
 * Manages a collection of {@code Row} instances.
 * 
 * @author		Fromentin Xavier, Schnell Michaël, Dervin Cyrielle, Brabant Quentin
 * @version		1.0
 */
public class Rows implements Iterable<Row>, Serializable {

	/**
	 * Needed to serialize this class.
	 * 
	 * @see		java.io.Serializable
	 */
	private static final long serialVersionUID = -7878908776660782654L;

	/**
	 * List of the instances of {@code Row}.
	 */
	protected List<Row> rows ;

	/**
	 * Builds an empty instance with no {@code Row}.
	 */
	public Rows() {
		super() ;
		this.rows = new ArrayList<Row>() ;
	}// Rows()

	/**
	 * Add an instance of {@code Row} at the specified index.
	 * 
	 * @param 	row
	 * 			instance of {@code Row} added
	 * 
	 * @param 	index
	 * 			insert position
	 * 
	 * @throws	InvalidIndexException
	 * 			if the given index is out of range
	 * 			{@code index < 0 || index >= getCount()}
	 */
	public void add(Row row, int index) throws InvalidIndexException {
		this.checkIndex(index) ;
		this.rows.add(index, row) ;
	}// add(Row, int)

	/**
	 * Prepends an instance of {@code Row}.
	 * 
	 * @param 	r
	 * 			instance of {@code Row} prepended
	 */
	public void push(Row r) {
		if (this.getCount() == 0) {
			this.rows.add(r) ;
		} else {
			this.rows.add(0, r) ;
		}// if
	}// push(Row)

	/**
	 * Appends an instance of {@code Row}.
	 * 
	 * @param 	r
	 * 			instance of {@code Row} appended
	 */
	public void append(Row r) {
		this.rows.add(r) ;
	}// append(Row)

	/**
	 * Removes the instance of {@code Row} found at the given index.
	 * 
	 * @param 	index
	 * 			index of the removed {@code Row}
	 * 
	 * @throws	InvalidIndexException
	 * 			if the given index is out of range
	 * 			{@code index < 0 || index >= getCount()}
	 */
	public void remove(int index) throws InvalidIndexException {
		this.checkIndex(index) ;
		this.rows.remove(index) ;
	}// remove(int)

	/**
	 * Removes all the instances matching the given {@code Row}.
	 * 
	 * @param 	p
	 * 			removed {@code Row}
	 */
	public void remove(Row p) {
		boolean stillPresent = this.rows.remove(p) ;
		while (stillPresent) {
			stillPresent = this.rows.remove(p) ;
		}// while
	}// remove(Row)

	/**
	 * Returns the number of instances of {@code Row} contained in this instance.
	 * 
	 * @return	Number of instances of {@code Row} contained in this instance
	 */
	public int getCount() {
		return this.rows.size() ;
	}// getCount()

	/**
	 * Returns an iterator for this instance.
	 * 
	 * <p>The iterator iterates over the document sequentially.
	 * 
	 * @return	an instance of {@code Iterator<Row>} for this instance
	 */
	@Override
	public Iterator<Row> iterator() {
		return this.rows.iterator() ;
	}// iterator()

	/**
	 * Returns a {@code ListIterator} for this instance.
	 * 
	 * <p>The ListIterator iterates over the rows in both direction.
	 * 
	 * @param   index
	 *          a position the iterator will begin at
	 * 
	 * @return	An instance of {@code ListIterator<Row>} for this instance
	 * 
	 * @throws	InvalidIndexException
	 * 			if the given index is out of range
	 * 			{@code index < 0 || index >= getCount()}
	 */
	public ListIterator<Row> listIterator(int index) throws InvalidIndexException {
		this.checkIndex(index) ;
		return this.rows.listIterator(index) ;
	}// listIterator(int)

	/**
	 * Checks if the given index is a valid index for this instance.
	 * 
	 * <p>If the index is valid, no exception is thrown.
	 * 
	 * @param 	index
	 * 			checked index
	 * 
	 * @throws 	InvalidIndexException
	 * 			if the given index is out of range
	 * 			{@code index < 0 || index >= getCount()}
	 */
	protected void checkIndex(int index) throws InvalidIndexException {
		if ((index < 0) || (index >= this.rows.size())) {
			throw new InvalidIndexException(index) ;
		}// if
	}// checkIndex(int)

}// class Rows